{
 "metadata": {
  "name": "",
  "signature": "sha256:67d86b39c594b2e56a0d2d1f57cb3baf20d15d27613ee13c9abcc1ca5c4f0a51"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {
      "slideshow": {
       "slide_type": "slide"
      }
     },
     "source": [
      "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Financial Time Series Visualization &mdash; Streaming Plots"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**PyData London 2015**"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Yves Hilpisch\n",
      "\n",
      "<a href='mailto:team@tpq.io'>team@tpq.io</a> | <a href='http://tpq.io'>http://tpq.io</a>\n",
      "\n",
      "The Python Quants GmbH"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Prerequisites"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import json\n",
      "import time\n",
      "import pandas as pd\n",
      "import requests\n",
      "import plotly.plotly as py\n",
      "import plotly.tools as tls \n",
      "from plotly.graph_objs import *\n",
      "import cufflinks"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Loading **Plotly credentials** from file."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pcreds = json.load(open('plotly_creds'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "py.sign_in('yves', pcreds['api_key'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Dummy Tick Data Source"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "For illustration purposes, we use this data source which provides **dummy tick data**."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tick_url = 'http://cloud.datapark.io:12500/prices'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "r = requests.get(tick_url)\n",
      "r.json()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Collecting Data from Source"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Collecting **data via a loop**."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df = pd.DataFrame()\n",
      "for _ in xrange(100):\n",
      "    r = requests.get(tick_url)\n",
      "    data = r.json()\n",
      "    df = df.append(pd.DataFrame(data['tick'],\n",
      "                                index=(data['tick']['time'],)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df.tail()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plotting with plotly"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Converting the `DataFrame` data into **Plotly compatible format**."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def df_to_iplot(df):\n",
      "    '''\n",
      "    Coverting a pandas DataFrame to Plotly interface.\n",
      "    '''\n",
      "    x = df.index.values\n",
      "    lines = {}\n",
      "    for key in df:\n",
      "        lines[key] = {}\n",
      "        lines[key][\"x\"] = x\n",
      "        lines[key][\"y\"] = df[key].values\n",
      "        lines[key][\"name\"] = key\n",
      "        # Appending all lines\n",
      "    lines_plotly=[lines[key] for key in df]\n",
      "    return lines_plotly"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "And **iplotting** it."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "py.iplot(df_to_iplot(df[['ask']]))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "More Simple with Cufflinks"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Cufflinks wraps the Plotly API for the `DataFrame` class."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df['ask'].iplot(world_readable=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Streaming Plot"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Plotting **data streams** is a bit more involved. We need a `Stream` object ..."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Get stream id from stream id list \n",
      "stream_ids = pcreds['stream_ids']\n",
      "\n",
      "# Make instance of stream id object \n",
      "stream_0 = Stream(\n",
      "    token=stream_ids[0],\n",
      "    maxpoints=150)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "... and a `Scatter` object which gets passed to a `Data` object."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "trace0 = Scatter(\n",
      "    x=[], y=[],\n",
      "    mode='lines+markers',\n",
      "    stream=stream_0,\n",
      "    name='price')\n",
      "\n",
      "dats = Data([trace0])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The rest then is again straightforward."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "layout = Layout(title='Streaming Plot')\n",
      "fig = Figure(data=dats, layout=layout)\n",
      "unique_url = py.plot(fig, filename='stream_plot', auto_open=False)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Embedding** the plot first ..."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tls.embed(unique_url)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "... **streaming** the data second."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s0 = py.Stream(stream_ids[0])\n",
      "s0.open()\n",
      "for i in xrange(250):\n",
      "    r = requests.get(tick_url)\n",
      "    data = r.json()\n",
      "    s0.write({'x': data['tick']['time'][11:-3], 'y': data['tick']['ask']})\n",
      "    time.sleep(0.25)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
      "\n",
      "<a href=\"http://tpq.io\" target=\"_blank\">http://tpq.io</a> | <a href=\"http://twitter.com/dyjh\" target=\"_blank\">@dyjh</a> | <a href=\"mailto:team@pqp.io\">team@tpq.io</a>\n",
      "\n",
      "**Quant Platform** |\n",
      "<a href=\"http://quant-platform.com\">http://quant-platform.com</a>\n",
      "\n",
      "**datapark.io** |\n",
      "<a href=\"http://datapark.io\">http://datapark.io</a>\n",
      "\n",
      "**Python for Finance** |\n",
      "<a href=\"http://python-for-finance.com\" target=\"_blank\">Python for Finance @ O'Reilly</a>\n",
      "\n",
      "**Derivatives Analytics with Python** |\n",
      "<a href=\"http://derivatives-analytics-with-python.com\" target=\"_blank\">Derivatives Analytics @ Wiley Finance</a>"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}